package com.icangya.DAO.impl;

import com.icangya.DAO.ISystemSettingDAO;
import com.icangya.domain.SystemSetting;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SystemSettingDAOImpl  extends  BaseDAO<SystemSetting> implements ISystemSettingDAO{


    @Override
    public SystemSetting getSystemZero() {
        String hql="FROM SystemSetting";
        List<SystemSetting> list= (List<SystemSetting>) getHibernateTemplate().find(hql);
        if(list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public int updateSystemSetting(SystemSetting systemSetting) {


        int result=getHibernateTemplate().execute(new HibernateCallback<Integer>() {
            @Override
            public Integer doInHibernate(Session session) throws HibernateException {

                String hql="update SystemSetting sy set sy.id=sy.id ";

                if(systemSetting.getUpdateNotice()!=null)
                    hql=hql +" ,sy.updateNotice='"+systemSetting.getUpdateNotice()+"'";

                if(systemSetting.getGamePlay()!=null)
                    hql=hql +" ,sy.gamePlay='"+systemSetting.getGamePlay()+"'";

                if(systemSetting.getKickSkill()!=null)
                    hql=hql+" ,sy.kickSkill='"+systemSetting.getKickSkill()+"'";

                hql=hql+" where sy.id=?";
                Query query=session.createQuery(hql);
                query.setParameter(0,systemSetting.getId());
                int ret=query.executeUpdate();
                return ret;
            }
        });

        return result;
    }
}
